package org.freedom.fastdevpt.utils;

import java.io.File;

import javax.servlet.ServletException;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Server {
	 final static Logger logger = LoggerFactory.getLogger(Server.class);

	public static void main(String[] args) throws ServletException {
		logger.info("tomcat server start running.....");
		Tomcat server = new Tomcat();
		server.setPort(8080);
		server.setBaseDir("/tmp");
		String web = new File("src/main/webapp").getAbsolutePath();
		// String webpath = System.getProperty("user.dir") + File.separator +
		// "WebContent";
		server.getHost().setAppBase(web);
		StandardContext ctx = (StandardContext) server.addWebapp("/", web);
		WebResourceRoot resource = new StandardRoot(ctx);
		File clas = new File("target/classes");
		resource.addPreResources(new DirResourceSet(resource, "/WEB-INF/classes", clas.getAbsolutePath(), "/"));
		ctx.setResources(resource);
		try {
			server.start();
		} catch (LifecycleException e) {
		
			e.printStackTrace();
		}
		logger.info("tomcat server is running.....");
		server.getServer().await();

	}

}
